# -*- coding: utf-8 -*-

# File Name： board_label
# Description :
# Author : lirui
# create_date： 2022/9/15
# Change Activity:
from modules.generate_label.obj.board_label import BoardLabel

CARD_NUMBER_SEQUENCE = [
    '01A', '01B', '01C', '01D',
    '02A', '02B', '02C', '02D',
    '03A', '03B', '03C', '03D'
]


class BoardLabelsGenerator:
    def __init__(self, xls_info):
        """

        Args:
            xls_info:
        """
        self.xls_info = xls_info
        self.labels_lst = self._generate_labels_lst(xls_info)
        self.labels_text_lst = self._gen_labels_text_lst(self.labels_lst)

    @staticmethod
    def _gen_labels_text_lst(labels_lst):
        labels_text_lst = []
        for label in labels_lst:
            text = label.label_text
            labels_text_lst.append(text)
        return labels_text_lst
        pass

    @staticmethod
    def _generate_labels_lst(xls_info):
        """

        Args:
            xls_info:

        Returns:

        """
        local_board_num = xls_info['本端板卡编号']
        local_board_num_lst = BoardLabelsGenerator.generate_card_number_sequence(local_board_num)
        opposite_board_num = xls_info['对端板卡编号']
        opposite_board_num_lst = BoardLabelsGenerator.generate_card_number_sequence(opposite_board_num)
        # if len(local_board_num_lst) != len(opposite_board_num_lst):
        #     print(local_board_num_lst)
        assert len(local_board_num_lst) == len(opposite_board_num_lst)
        board_label_lst = []
        for index, local_card_num in enumerate(local_board_num_lst):
            if len(local_card_num) == 0:  # filter zero rows
                continue
            opposite_card_num = opposite_board_num_lst[index]
            board_label = BoardLabel(cable_num=xls_info['数量'],
                                     cable_type=xls_info['线缆类型'],
                                     local_addr=xls_info['本端地址'],
                                     local_u=xls_info['本端U数'],
                                     local_board_num=local_card_num,
                                     opposite_addr=xls_info['对端地址'],
                                     opposite_u=xls_info['对端U数'],
                                     opposite_board_num=opposite_card_num,
                                     local_port_num=xls_info['本端端口号'],
                                     opposite_port_num=xls_info['对端端口号'])
            board_label_lst.append(board_label)
        return board_label_lst

    @staticmethod
    def generate_card_number_sequence(card_num):
        """

        Args:
            card_num:

        Returns:

        """
        ret_card_num_list = []
        if '-' in card_num:
            start_num, end_num = card_num.split('-')
            star_collect = False
            for seq in CARD_NUMBER_SEQUENCE:
                if start_num == seq:
                    star_collect = True
                if star_collect:
                    ret_card_num_list.append(seq)
                if end_num == seq:
                    star_collect = False
        else:
            ret_card_num_list.append(card_num)
        return ret_card_num_list
